﻿using QuanLyThuVien.BLL;
using QuanLyThuVien.DTO;
using QuanLyThuVien.Supporter;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QuanLyThuVien.GUI
{
    public partial class GUITraCuuSach : DevComponents.DotNetBar.Office2007Form
    {
        private BLLTheLoai _bllTheLoaiSach;
        private BLLTacGia _bllTacGia;
        private BLLSach _bllSach;

        private List<DTOTheLoai> _DSTheLoai;
        private List<DTOTacGia> _DSTacGia;

        //----------------------------------------------------------------------
        //Desc: constructor mặc định
        //----------------------------------------------------------------------
        public GUITraCuuSach()
        {
            InitializeComponent();

            _bllTheLoaiSach = new BLLTheLoai();
            _bllTacGia = new BLLTacGia();
            _bllSach = new BLLSach();
            _DSTheLoai = new List<DTOTheLoai>();
            _DSTacGia = new List<DTOTacGia>();

            try
            {
                _cboTriGia.Items.Add("0 -- 50000");
                _cboTriGia.Items.Add("50000 -- 100000");
                _cboTriGia.Items.Add("100000 -- 200000");
                _cboTriGia.Items.Add("200000 --");
                _cboTriGia.SelectedIndex = 0;
            }
            catch { }
        }

        //----------------------------------------------------------------------
        //Desc: hiển thị danh sách thể loại
        //----------------------------------------------------------------------
        public void HienThiDSTheLoai()
        {
            _DSTheLoai = _bllTheLoaiSach.LayDSTheLoai();
            if (_DSTheLoai != null)
            {
                for (int i = 0; i < _DSTheLoai.Count; i++)
                {
                    try
                    {
                        _cboTheLoai.Items.Add(_DSTheLoai[i].TenTheLoai);
                    }
                    catch
                    { }
                }
                try
                {
                    _cboTheLoai.SelectedIndex = 0;
                }
                catch
                { }
            }

        }

        //----------------------------------------------------------------------
        //Desc: hiển thị danh sách tác giả
        //----------------------------------------------------------------------
        public void HienThiDSTacGia()
        {
            _DSTacGia = _bllTacGia.LayDSTacGia();
            if (_DSTacGia != null)
            {
                for (int i = 0; i < _DSTacGia.Count; i++)
                {
                    try
                    {
                        _cboTacGia.Items.Add(_DSTacGia[i].TenTG);
                    }
                    catch
                    { }
                }
                try
                {
                    _cboTacGia.SelectedIndex = 0;
                }
                catch
                { }
            }
        }

        //----------------------------------------------------------------------
        //Desc: hiển thị thông tin sách
        //----------------------------------------------------------------------
        private void HienThiThongTinSach()
        {
            if (_dgwDSSach.CurrentRow != null)
            {
                string masach = "";
                try
                {
                    masach = (string)_dgwDSSach.Rows[_dgwDSSach.CurrentRow.Index].Cells[0].Value;
                }
                catch { }
                if (masach != null && masach != "")
                {
                    string madausach = masach.Substring(0, 3);
                    BLLDauSach _bllDauSach = new BLLDauSach();
                    DTODauSach dausach = _bllDauSach.LayDauSach(madausach);
                    if (dausach == null)
                        MessageBox.Show("Không lấy được thông tin sách");
                    else
                    {
                        BLLSach _bllSach = new BLLSach();
                        DTOSach sach = _bllSach.LaySach(masach);
                        if (sach == null)
                            MessageBox.Show("Không lấy được thông tin sách");
                        else
                        {
                            if (UserManager.User != null && (UserManager.User.LoaiNguoiDung == LOAI_NGUOI_DUNG.NguoiQuanTri
                                || UserManager.User.LoaiNguoiDung == LOAI_NGUOI_DUNG.ThuThu))
                                new GUISach(THAOTAC.SUA, dausach, sach).Show();
                            else
                                new GUISach(THAOTAC.XEM, dausach, sach).Show();
                        }
                    }
                }
            }
        }

        //----------------------------------------------------------------------
        //Desc: xử lý sự kiện form load
        //----------------------------------------------------------------------
        private void GUITraCuuSach_Load(object sender, EventArgs e)
        {
            HienThiDSTheLoai();
            HienThiDSTacGia();
            _dtNgayNhap.Value = DateTime.Today;
        }

        //----------------------------------------------------------------------
        //Desc: xử lý sự kiện click button Thoat
        //----------------------------------------------------------------------
        private void _btnThoat_Click(object sender, EventArgs e)
        {
            Close();
        }

        //----------------------------------------------------------------------
        //Desc: xử lý sự kiện click button TraCuu
        //----------------------------------------------------------------------
        private void _btnTraCuu_Click(object sender, EventArgs e)
        {
            if (_chbMaSach.Checked == false && _chbTenSach.Checked == false && _chbTheLoai.Checked == false
                && _chbTacGia.Checked == false && _chbNamXB.Checked == false && _chbNhaXB.Checked == false
                && _chbNgayNhap.Checked == false && _chbTriGia.Checked == false)
                MessageBox.Show("Chưa chọn thông tin tra cứu");
            else
            {
                string masach;
                if (_chbMaSach.Checked && _txtMaSach.Text != "")
                    masach = Utils.ClearSpace(_txtMaSach.Text);
                else
                    masach = null;

                string tensach;
                if (_chbTenSach.Checked && _txtTenSach.Text != "")
                    tensach = Utils.ClearSpace(_txtTenSach.Text);
                else
                    tensach = null;

                string matl;
                if (_chbTheLoai.Checked && _cboTheLoai.Text != "")
                {
                    try
                    {
                        matl = _DSTheLoai[_cboTheLoai.SelectedIndex].MaTheLoai;
                    }
                    catch { matl = null; }
                }
                else
                    matl = null;

                string matg;
                if (_chbTacGia.Checked && _cboTacGia.Text != "")
                {
                    try
                    {
                        matg = _DSTacGia[_cboTacGia.SelectedIndex].MaTG;
                    }
                    catch { matg = null; }
                }
                else
                    matg = null;

                long namxb;
                if (_chbNamXB.Checked && _txtNamXB.Text != "")
                {
                    try
                    {
                        namxb = long.Parse(_txtNamXB.Text);
                    }
                    catch 
                    {
                        namxb = -1;
                        MessageBox.Show("Nhập sai năm xuất bản");
                        return;
                    }
                }
                else
                    namxb = -1;

                string nhaxb;
                if (_chbNhaXB.Checked && _txtNhaXB.Text != "")
                    nhaxb = Utils.ClearSpace(_txtNhaXB.Text);
                else
                    nhaxb = null;

                DateTime ngaynhap;
                object objNgayNhap;
                if (_chbNgayNhap.Checked && _dtNgayNhap.Text != "")
                {
                    ngaynhap = _dtNgayNhap.Value;
                    objNgayNhap = (object)ngaynhap;
                }
                else
                    objNgayNhap = null;

                double trigiamin;
                double trigiamax;
                if (_chbTriGia.Checked)
                {
                    int index = -1;
                    try
                    {
                        index = _cboTriGia.SelectedIndex;
                    }
                    catch { }
                    switch (index)
                    {
                        case 0:
                            trigiamin = 0;
                            trigiamax = 50000;
                            break;
                        case 1:
                            trigiamin = 50000;
                            trigiamax = 100000;
                            break;
                        case 2:
                            trigiamin = 100000;
                            trigiamax = 200000;
                            break;
                        case 3:
                            trigiamin = 200000;
                            trigiamax = -1;
                            break;
                        default:
                            trigiamin = 0;
                            trigiamax = -1;
                            break;
                    }
                }
                else
                {
                    trigiamin = 0;
                    trigiamax = -1;
                }

                DataTable data = _bllSach.TraCuuSach(masach, tensach, matl, matg, namxb, nhaxb, objNgayNhap, trigiamin, trigiamax);
                if (data == null)
                    MessageBox.Show("Tra cứu thất bại");
                else
                {
                    try
                    {
                        _dgwDSSach.Rows.Clear();
                    }
                    catch { }
                    _dgwDSSach.DataSource = data;
                    MessageBox.Show("Tìm được " + data.Rows.Count.ToString() + " sách");
                }
            }
        }

        //----------------------------------------------------------------------
        //Desc: xử lý sự kiện double click cell của datagridview
        //----------------------------------------------------------------------
        private void dgwDSSach_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            HienThiThongTinSach();
        }

        //----------------------------------------------------------------------
        //Desc: xử lý sự kiện key press
        //----------------------------------------------------------------------
        private void txtNhapSo_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e != null)
            {
                if (Utils.CheckKeyCode(e.KeyChar))
                    e.Handled = false;
                else
                    e.Handled = true;
            }
        }

        //----------------------------------------------------------------------
        //Desc: xử lý sự kiện key press
        //----------------------------------------------------------------------
        private void Special_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Utils.IsSpecialKey(e.KeyChar))
                e.Handled = true;
            else
                e.Handled = false;
        }
    }
}
